package com.xintonghua.bussiness.ui.home;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.api.HttpCent;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.TaskBean;
import com.xintonghua.bussiness.bean.TaskBeans;
import com.xintonghua.bussiness.ui.fragment.cube.DialogStartAndEndTimeActivity;
import com.xintonghua.bussiness.util.JsonUtil;
import com.xintonghua.bussiness.util.JumpUtils;
import com.xintonghua.bussiness.util.MyState;
import com.xintonghua.bussiness.util.MyUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.addapp.pickers.listeners.OnItemPickListener;
import cn.addapp.pickers.picker.SinglePicker;

/**
 * case "今日任务":
 * //改变数据
 * <p>
 * taskType = "0";
 * getData();
 * title.setText("今日任务");
 * break;
 * case "本周任务":
 * taskType = "1";
 * getData();
 * //改变数据
 * title.setText("本周任务");
 * break;
 * case "本月任务":
 * taskType = "2";
 * getData();
 * //改变数据
 * title.setText("本月任务");
 * break;
 * case "本季任务":
 * taskType = "3";
 * title.setText("本季任务");
 */
public class TaskActivity extends BaseActivity implements initData {

    @BindView(R.id.tv_totalTaskNums)
    TextView tvTotalTaskNums;
    @BindView(R.id.tvNowTaskCompleted)
    TextView tvNowTaskCompleted;
    @BindView(R.id.tl_performance)
    TabLayout tlPerformance;
    String taskType; //0  今日任务  1 本周任务  2本月任务  3本季任务  4时间筛选
    @BindView(android.R.id.title)
    TextView title;
    @BindView(android.R.id.home)
    ImageView home;
    //    @BindView(R.id.action)
//    TextView action;
//    @BindView(R.id.action_image)
//    ImageView actionImage;

    @BindView(R.id.fl_group2)
    FrameLayout flGroup2;
    @BindView(R.id.ll_dqwc)
    LinearLayout llDqwc;
//    RelativeLayout title_bbg;


    @BindView(R.id.tv_starttime_and_endtime)
    TextView tv_starttime_and_endtime;
    //    TextView titles;
    FragmentManager manager;
    String shopUserId;//如果是空表示看自己的

    @BindView(R.id.tv_totalTaskTitle)
    TextView tvTotalTaskTitle;
    String starttime = "";
    String endtime = "";
    String money = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_task2);
        ButterKnife.bind(this);
        shopUserId = getIntent().getStringExtra("shopUserId");
        starttime = getIntent().getStringExtra("starttime");
        endtime = getIntent().getStringExtra("endtime");
        money = getIntent().getStringExtra("money");
        tvTotalTaskNums.setText(money);
//        onSimpleActionBar();
        String taskName = getIntent().getStringExtra("taskName");
        title.setText(taskName);
//        setSimpleActionBar(""+taskName);
////        title_bbg = (RelativeLayout) findViewById(R.id.title_bg);
////        titles = (TextView) findViewById(R.id.title);
        taskType = getIntent().getStringExtra("taskType");
        if (taskType.equals("4")) {
            llDqwc.setVisibility(View.GONE);
            tv_starttime_and_endtime.setVisibility(View.VISIBLE);
            tv_starttime_and_endtime.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    JumpUtils.jumpActivityForResult(TaskActivity.this, DialogStartAndEndTimeActivity.class, null, MyState.requestL3000);
                }
            });
        }


        manager = getSupportFragmentManager();
        initTabLayout("0", "0");

        Log.i("aaa", "taskType===>" + taskType);
        final List<String> taskNames = initTaskName();
        title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!taskType.equals("4"))
                    showTaskChoose(taskNames);
            }
        });
        home.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        if (!taskType.equals("4"))
            getData();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            if (requestCode == MyState.requestL3000 && resultCode == Activity.RESULT_OK) {
                if (data != null) {
                    if (data != null) {
                        starttime = data.getStringExtra("starttime");
                        endtime = data.getStringExtra("endtime");

                        tv_starttime_and_endtime.setText(starttime + " 至 " + endtime);
//                          svScrollview.setVisibility(View.VISIBLE);
//                          getNetData();
                        getData();
                    }
                }
            }
        } catch (Exception e) {

        }
    }

    public List<String> initTaskName() {
        List<String> list = new ArrayList<>();
        list.add("今日目标");
        list.add("本周目标");
        list.add("本月目标");
        list.add("本季目标");
        return list;
    }

    TextView itemTabTaskMoney1;
    TextView itemTabTaskMoney2;

    @Override
    public void initTabLayout(String productMoney, String serviceMoney) {

        //tab居中显示
        tlPerformance.setTabGravity(TabLayout.GRAVITY_CENTER);
        //tab的字体选择器,默认黑色,选择时红色
        tlPerformance.setTabTextColors(Color.BLACK, Color.RED);
        //tab的下划线颜色,默认是粉红色,如果要自定义选中效果,则可以将下划线设置为和背景色一样.
        tlPerformance.setSelectedTabIndicatorColor(Color.RED);
        tlPerformance.post(new Runnable() {
            @Override
            public void run() {
                //MyUtils.setTabLine(TaskActivity.this, tlPerformance, 20, 20);
            }
        });
//        tlPerformance.setPadding(20,0,20,0);
        View view1 = LayoutInflater.from(this).inflate(R.layout.item_tablayout_task, null);
        itemTabTaskMoney1 = view1.findViewById(R.id.itemTabTaskMoney);
        TextView itemTabTaskType1 = view1.findViewById(R.id.itemTabTaskType);
        itemTabTaskMoney1.setText(productMoney);
        itemTabTaskType1.setText("销售业绩");

        View view2 = LayoutInflater.from(this).inflate(R.layout.item_tablayout_task, null);
        itemTabTaskMoney2 = view2.findViewById(R.id.itemTabTaskMoney);
        TextView itemTabTaskType2 = view2.findViewById(R.id.itemTabTaskType);
        itemTabTaskMoney2.setText(serviceMoney);
        itemTabTaskType2.setText("消耗业绩");

        tlPerformance.addTab(tlPerformance.newTab().setCustomView(view1));
        tlPerformance.addTab(tlPerformance.newTab().setCustomView(view2));
        itemTabTaskMoney1.setTextColor(Color.RED);
        tlPerformance.getTabAt(0);
        tlPerformance.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                if (position == 0) {
                    itemTabTaskMoney1.setTextColor(Color.RED);
                    itemTabTaskMoney2.setTextColor(Color.BLACK);
                    initFragment();
                } else if (position == 1) {
                    itemTabTaskMoney1.setTextColor(Color.BLACK);
                    itemTabTaskMoney2.setTextColor(Color.RED);
                    initFragment();
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void showTaskChoose(final List<String> pList) {
        SinglePicker<String> picker = new SinglePicker<>(this, pList);
        picker.setCanLoop(false);//不禁用循环
        picker.setLineVisible(true);
        picker.setShadowVisible(true);
        picker.setTextSize(18);
        picker.setSelectedIndex(1);
        picker.setWheelModeEnable(true);
        //启用权重 setWeightWidth 才起作用
        picker.setLabel(null);
        picker.setWeightEnable(true);
        picker.setWeightWidth(1);
        picker.setSelectedTextColor(0xFF279BAA);//前四位值是透明度
        picker.setUnSelectedTextColor(0xFF999999);
        picker.setOnItemPickListener(new OnItemPickListener<String>() {
            @Override
            public void onItemPicked(int index, String item) {
//                titles.setText(item);
                switch (item) {
                    case "今日目标":
                        //改变数据

                        taskType = "0";
                        getData();
                        title.setText("今日目标");
                        break;
                    case "本周目标":
                        taskType = "1";
                        getData();
                        //改变数据
                        title.setText("本周目标");
                        break;
                    case "本月目标":
                        taskType = "2";
                        getData();
                        //改变数据
                        title.setText("本月目标");
                        break;
                    case "本季目标":
                        taskType = "3";
                        getData();
                        title.setText("本季目标");
                        //改变数据
                        break;
                }
            }
        });
        picker.show();
    }

    @Override
    public void initFragment() {

        if (bean == null) {
            return;
        }
        TaskFragment fragment0 = new TaskFragment();
        TaskFragment fragment1 = new TaskFragment();
        Bundle bun = new Bundle();
        bun.putSerializable("data0", (Serializable) bean1.getList());
        bun.putSerializable("data1", (Serializable) bean.getList());
        bun.putString("type", "0");
        fragment0.setArguments(bun);
        Bundle bun1 = new Bundle();
        bun1.putSerializable("data0", (Serializable) bean1.getList());
        bun1.putSerializable("data1", (Serializable) bean.getList());
        bun1.putString("type", "1");
        fragment1.setArguments(bun1);
        if (tlPerformance.getSelectedTabPosition() == 0) {
            FragmentTransaction transaction = manager.beginTransaction();
            transaction.replace(R.id.fl_group2, fragment0);
            transaction.commit();
        } else {
            FragmentTransaction transaction = manager.beginTransaction();
            transaction.replace(R.id.fl_group2, fragment1);
            transaction.commit();
        }


    }

    TaskBean bean;
    TaskBeans bean1;

    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        switch (order) {
            case MyState.requestOne:
                try {
                    bean = JsonUtil.getEntityByJsonString(data.toString(), TaskBean.class);
                    initFragment();
                    initViewData(bean.getTotal());
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.i("aaa", "出错了");
                }
                break;
            case MyState.requestTwo:
                try {
                    bean1 = JsonUtil.getEntityByJsonString(data.toString(), TaskBeans.class);
                    initFragment();
                    initViewData(bean1.getTotal());
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.i("aaa", "出错了");
                }
                break;
        }
    }


    @Override
    public void getData() {
        HttpCent.getInstance(this).getTaskDetails(1, shopUserId, "" + taskType, starttime, endtime, this, MyState.requestOne);
        HttpCent.getInstance(this).getSaleDetails(1, shopUserId, "" + taskType, starttime, endtime, this, MyState.requestTwo);
    }

    @Override
    public void initViewData(String total) {
        if (!taskType.equals("4")) {
            //tvTotalTaskNums.setText("" + bean.getTotal());
            tvNowTaskCompleted.setText(bean1.getOkTotal());
        } else {
            tvTotalTaskNums.setText(total);
            tvTotalTaskTitle.setText("已完成:");
        }

        itemTabTaskMoney1.setText(bean1.getOkTotal());
        itemTabTaskMoney2.setText(bean.getTotal());
    }
}

interface initData {
    void getData();

    void initFragment();

    void initTabLayout(String productMoney, String serviceMoney);


    void initViewData(String total);
}